from pymouse import PyMouse
import time
import pyautogui
import cv2
import numpy as np
from matplotlib import pyplot as plt 

m = PyMouse()

##  进入关卡时的固定操作
def inGame():
    m.click(1322, 278) #关卡选择坐标
    time.sleep(5)
    m.click(632, 209)   #选择职介
    time.sleep(5)
##   角色选择 TODO    
    m.click(756, 384)   #选择角色
    time.sleep(5)
    m.click(1742, 999) #开始任务

##  释放 技能
def doSkill(character, skill):
    x_list =[[ 0, 0,          0 ],          
                 [ 0, 0,        843 ],
                 [ 0, 1181, 1304 ],]
    y_list =[[ 0, 0,       0 ],
                 [ 0, 0,     872 ],
                 [ 0, 868, 870 ],]
    for i in range(1, 3):
        for j in range(1, 3):
            if (character == i):
                if (skill == j):
                    m.click(x_list[i-1][j-1], y_list[i-1][j-1])

##   点击attack
def attack():
    m.click(1658, 908)
    time.sleep(5)

## 指令卡选择
def chooseCard(index):
    x_list = [193, 570, 945, 1317, 1699]
    for i in range(1, 6):
        if (index == i):
            m.click(x_list[i-1], 760)
    time.sleep(3)

##  截取结束后的标志
def isEndScreenshot():
    img = pyautogui.screenshot(region=[1578, 949, 150, 53]) # x,y,w,h
    img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)
    return img

##  截取是否能选卡攻击的标志
def canAttack():
    img = pyautogui.screenshot(region=[112, 268, 347, 69]) # x,y,w,h
    img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)
    return img

##  截取游戏结束的第二个标志
def isEnd2():
    img = pyautogui.screenshot(region=[171, 135, 339, 55]) # x,y,w,h
    img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)
    return img

##  指令卡 截图 保存
def cardScreenshot(index):
    pos_list = [
        [90, 630, 220, 300],
        [460, 630, 220, 300],
        [830 , 630, 220, 300],
        [1200, 630, 220, 300],
        [1580, 630, 220, 300]
        ]
    for i in range(1, 6):
        img = pyautogui.screenshot(region=[pos_list[i-1][0], pos_list[i-1][1],
                                           pos_list[i-1][2], pos_list[i-1][3]]) # x,y,w,h
        img.save(str(index) + str(i)  + '.png')

## 指令卡 截图 不保存
def cardScreenshotTouse(index):
    pos_list = [
        [90, 630, 220, 300],
        [460, 630, 220, 300],
        [830 , 630, 220, 300],
        [1200, 630, 220, 300],
        [1580, 630, 220, 300]
        ]
    img = pyautogui.screenshot(region=[pos_list[index-1][0], pos_list[index-1][1],
                                           pos_list[index-1][2], pos_list[index-1][3]]) # x,y,w,h
    img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)
    return img

##  保存三个人的15张卡的图片
def saveCard():
    ttl = 3
    while(ttl != 0):
        attack()
        cardScreenshot(ttl)
        chooseCard(1)
        chooseCard(2)
        chooseCard(3)
        ttl = ttl - 1
        time.sleep(30)

## 计算汉明距离 
def Hamming_distance(hash1,hash2): 
    num = 0
    for index in range(len(hash1)): 
        if (hash1[index] != hash2[index]): 
            num += 1
    return num

# 输入灰度图，返回hash 
def getHash(image): 
    avreage = np.mean(image) 
    hash = [] 
    for i in range(image.shape[0]): 
        for j in range(image.shape[1]): 
            if image[i,j] > avreage: 
                hash.append(1) 
            else: 
                hash.append(0) 
    return hash

##  得到两张图片的相似度
def classify_pHash(image1,image2): 
    image1 = cv2.resize(image1,(32,32)) 
    image2 = cv2.resize(image2,(32,32)) 
    gray1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY) 
    gray2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY) 
    # 将灰度图转为浮点型，再进行dct变换 
    dct1 = cv2.dct(np.float32(gray1)) 
    dct2 = cv2.dct(np.float32(gray2)) 
    # 取左上角的8*8，这些代表图片的最低频率 
    # 这个操作等价于c++中利用opencv实现的掩码操作 
    # 在python中进行掩码操作，可以直接这样取出图像矩阵的某一部分 
    dct1_roi = dct1[0:8,0:8] 
    dct2_roi = dct2[0:8,0:8] 
    hash1 = getHash(dct1_roi) 
    hash2 = getHash(dct2_roi) 
    return Hamming_distance(hash1,hash2) 

saveCard()
